package com.xumt.hwHMSdemo;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;


import com.xumt.hwHMSdemo.Adapter.CommodityAdapter;
import com.xumt.hwHMSdemo.Adapter.ShoppingcarAdapter;
import com.xumt.hwHMSdemo.Adapter.TopicRecycleViewDivider;
import com.xumt.hwHMSdemo.cannectSb.HttpRequest;

import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONStringer;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

public class Newshoppingcar extends AppCompatActivity {

    private String shopname;
    private String shopgoing_id;
    private String[] commdity_message=new String[200];
    private List<String> shopnameliset =new ArrayList<>();
    private List<String> disrciptionlist =new ArrayList<>();
    private List<String> commdity_pricelist =new ArrayList<>();
    private List<List<Map<String,Object>>> Shopcargoring=new ArrayList<>();
    private  JSONArray jsonArray2[]=new JSONArray[200];
    private String test;
    public TextView timeTextView;
    private final Timer timer = new Timer();
    private TimerTask task;
    public static int Orderid;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_newshoppingcar);
        findname();
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        try {
            JSONArray jsonArray= new JSONArray(shopname);
            JSONArray jsonArray1= new JSONArray(shopgoing_id);
            Log.i("lenth","go"+jsonArray1.length());
            String a[]= new String[jsonArray.length()];
            int shopgoind_idGp[]=new int[jsonArray1.length()];
            for (int i=0;i<jsonArray.length();i++)
            {
                JSONObject jsonObject=jsonArray.getJSONObject(i);
                JSONObject jsonObject1=jsonArray1.getJSONObject(i);
                a[i]=(jsonObject.getString("shop"));
                shopgoind_idGp[i]=(jsonObject1.getInt("id"));
                shopnameliset.add(a[i]);
            }
            Log.i("idis","go"+shopgoind_idGp[0]);
            Log.i("idis","go"+a[0]);
            JSONArray jsonArray3[];
            String commdit[]=new String[shopgoind_idGp.length];
            findshopgoind(shopgoind_idGp,shopgoind_idGp.length);
            Thread.sleep(2000);
            for (int i=0;i<shopgoind_idGp.length;i++)
            {
                if (commdity_message[i]==null)
                {
                    break;
                }
                jsonArray2[i]= new JSONArray(commdity_message[i]);
            }
            for (int i=0;i<shopgoind_idGp.length;i++)
            {
                List<Map<String,Object>> list=new ArrayList<>();
                for (int j=0;j<jsonArray2[i].length();j++)
                {
                    JSONObject jsonObject=jsonArray2[i].getJSONObject(j);
                    Map<String,Object> map=new HashMap<String, Object>();
                    map.put("name",jsonObject.getString("name"));
                    map.put("discription",jsonObject.getString("description"));
                    map.put("price",jsonObject.getInt("price"));
                    map.put("number",jsonObject.getString("number"));
                    Context ctx=getBaseContext();
                    int resId = getResources().getIdentifier(jsonObject.getString("image"), "drawable" ,ctx.getPackageName());
                    map.put("image",resId);
                    list.add(map);
                }
                Shopcargoring.add(list);
                Log.i("mapmessage:","json"+Shopcargoring.get(0).get(0));

            }
            Log.i("mapmessage:","oOK"+Shopcargoring.get(0).get(0).get("name"));




        }catch (Exception e)
        {
            e.printStackTrace();
        }

        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        ShoppingcarAdapter adapter=new ShoppingcarAdapter(this,Shopcargoring,shopnameliset);
        RecyclerView recyclerView= findViewById(R.id.Shopveiw);
//            recyclerView.addItemDecoration(new TopicRecycleViewDivider(this,R.drawable.bandline2));
//        StaggeredGridLayoutManager stagger = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
//        recyclerView.setLayoutManager(stagger);
        LinearLayoutManager layoutManager=new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(adapter);

        TextView allprice=(TextView)findViewById(R.id.allprice);

        Handler handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                // TODO Auto-generated method stub
                allprice.setText(String.valueOf(CommodityAdapter.allprice));
                super.handleMessage(msg);
            }
        };
        task = new TimerTask() {
            @Override
            public void run() {
                // TODO Auto-generated method stub
                Message message = new Message();
                message.what = 1;
                handler.sendMessage(message);
            }
        };


        timer.schedule(task, 100, 100);
        Intent intent=new Intent(this,ChanceAdress.class);
        Button submit=(Button)findViewById(R.id.submitbutton);
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Orderid=(int) (Math.random()*9999+1);
                submitorder(CommodityAdapter.buylist,CommodityAdapter.numberlist,Orderid);
                Log.i("test","buylist"+CommodityAdapter.buylist);
                startActivity(intent);

            }
        });




    }



    public void submitorder(List<String>buylist,List<String>numberlist,int ordid)
    {

        HttpRequest request = new HttpRequest();
        new Thread() {
            public void run() {
                try {
                    test=request.sendPost("http://169.254.132.31:8081/Commdity/insertorder","buylist="+buylist.toString()+"&"+"ordid="+ordid+"&"+"numberlist="+numberlist.toString());

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }
    
    public void findname()
    {

        HttpRequest request = new HttpRequest();
        new Thread() {
            public void run() {
                try {
                    shopname=request.sendGet("http://169.254.132.31:8081/Commdity/findshopname","name="+Login2.usernamen);
                    shopgoing_id=request.sendGet("http://169.254.132.31:8081/Commdity/findshopcard2_id","username="+Login2.usernamen);

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();

   }

    public void findshopgoind(int id[],int lenth)
    {

        HttpRequest request = new HttpRequest();
        new Thread() {
            public void run() {
                try {
                   for (int i=0;i<lenth;i++)
                   {
                       commdity_message[i]=request.sendGet("http://169.254.132.31:8081/Commdity/findshopcard1_number","id="+id[i]);
                   }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();

    }


   }
